using PaperPlaneTools;
using System;
using UnityEngine;

namespace Voodoo.Sauce.Internal.AppRater
{
	public class AndroidAppRater : MonoBehaviour, IAlertPlatformAdapter
	{
		private const string TAG = "AndroidAppRater";

		private Action _positiveCallback;

		private Action _neutralCallback;

		private Action _negativeCallback;

		private Action _dismissCallback;

		private const string unityActivityClass = "com.unity3d.player.UnityPlayer";

		private const string voodooAppRaterClass = "io.voodoo.voodooapprater.AppRater";

		private void Awake()
		{
			base.gameObject.name = "VoodooAppRater";
			VoodooLog.Log("AndroidAppRater", "Setting Android's AppRater custom alert");
			AppRater.SetCustomAlert(this);
		}

		public void SetOnDismiss(Action action)
		{
			_dismissCallback = action;
		}

		public void Show(Alert alert)
		{
			VoodooLog.Log("AndroidAppRater", "Showing rate pop up.");
			ShowNativeAlertDialog();
			_positiveCallback = alert.PositiveButton.Handler;
			_neutralCallback = alert.NeutralButton.Handler;
			_negativeCallback = alert.NegativeButton.Handler;
		}

		public void Dismiss()
		{
			VoodooLog.Log("AndroidAppRater", "Dismissing rate pop up.");
			if (_dismissCallback != null)
			{
				_dismissCallback();
			}
		}

		public void HandleEvent(string eventName, string value)
		{
			VoodooLog.Log("AndroidAppRater", "Native event : " + eventName + " -> " + value);
		}

		private static void ShowNativeAlertDialog()
		{
			AndroidJavaClass androidJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
			AndroidJavaObject @static = androidJavaClass.GetStatic<AndroidJavaObject>("currentActivity");
			AndroidJavaClass androidJavaClass2 = new AndroidJavaClass("io.voodoo.voodooapprater.AppRater");
			androidJavaClass2.CallStatic("Show", @static, @static, "Enjoying " + Application.productName + "?", "Tap a star to rate it on the store.", "Submit", "Not Now");
		}

		public void OnPositiveButtonClick(string ratingString)
		{
			int num = int.Parse(ratingString);
			VoodooLog.Log("AndroidAppRater", "OnPositiveButtonClick: " + num);
			if (num >= 4)
			{
				if (_positiveCallback != null)
				{
					_positiveCallback();
				}
			}
			else
			{
				if (_negativeCallback != null)
				{
					_negativeCallback();
				}
				Application.OpenURL("mailto:support@voodoo.io?subject=" + Application.productName + "%20Issue&body=Tell%20us%20what%20you%20didn't%20like%20:%20");
			}
			Dismiss();
		}

		public void OnNeutralButtonClick()
		{
			VoodooLog.Log("AndroidAppRater", "OnNeutralButtonClick");
			if (_neutralCallback != null)
			{
				_neutralCallback();
			}
			Dismiss();
		}
	}
}
